package org.tigr.htc.server;

/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.util.HashMap;

import org.apache.log4j.Logger;
import org.tigr.htc.cmd.Command;
import org.tigr.htc.common.HTCConfig;
import org.tigr.htc.server.condor.CondorJobMonitor;
import org.tigr.htc.server.sge.SgeJobMonitor;

/**
 * The <b>JobMonitorFactory</b> class is used to build and return instances of job
 * monitors for a specific grid implementation. These monitors can then be used by
 * runners to register commands which are launched on the grid.
 *
 */
public class JobMonitorFactory {
    static Logger log = Logger.getLogger(JobMonitorFactory.class);
    private static HashMap monitors = new HashMap();
    
    public static final String CONDOR = "condor";
    public static final String SGE = "sge";
    
    /**
     * The method <code>getMonitor</code> returns the monitor for the type of grid 
     * specified by the grid type in config file. If the monitor does not exists, one is 
     * created before it is returned.
     * 
     * @param cmd a <code>Command</code> which needs to be monitored
     * 
     * @return a <code>IJobMonitor</code> that knows how to monitor jobs for the grid to
     * which the command is sent.
     */
    public static IJobMonitor getMonitor(Command cmd) {
        IJobMonitor monitor = null;
        String gridType = HTCConfig.getProperty("grid.type");
        if (gridType.equals(CONDOR)) {
            log.debug("Returning monitor for grid type: " + gridType);
            if (monitors.containsKey(CONDOR)) {
                monitor = (IJobMonitor) monitors.get(CONDOR);
            } else {
                log.debug("Creating a monitor as one does not exist.");
                monitor = new CondorJobMonitor();
                monitors.put(CONDOR, monitor);
            }
        } 
        else if (gridType.equals(SGE)) {
            log.debug("Returning monitor for grid type: " + gridType);
            if (monitors.containsKey(SGE)) {
                monitor = (IJobMonitor) monitors.get(SGE);
            } else {
                log.debug("Creating a monitor as one does not exist.");
                // Create the monitor and start it.
                monitor = new SgeJobMonitor();
                Thread t = new Thread(monitor);
                t.setName("SGE monitoring thread");
                t.setDaemon(true);  // Set the monitor thread to be a daemon thread to 
                                    // ensure that it dies when the JVM exits
                t.start();
                monitors.put(SGE, monitor);
            }
        }
        
        return monitor;
    }
    
    /**
     * @param cmd
     * @param monitor
     */
    public static void releaseMonitor(Command cmd, IJobMonitor monitor) {
        String gridType = HTCConfig.getProperty("grid.type");
        monitor.setActive(false);
        monitors.remove(gridType);
    }
}
